<template>
	<view class="content">
		<mumu-get-qrcode @success='qrcodeSucess' @error="qrcodeError"></mumu-get-qrcode>
	</view>
</template>

<script>
	import { getUInfo } from '@/util/util.js'
	import mumuGetQrcode from '@/uni_modules/mumu-getQrcode/components/mumu-getQrcode/mumu-getQrcode.vue'
	export default {
		components: { mumuGetQrcode },
		data() {
			return {
				type: '', // 首页传过来的类型 1 优惠券码 2 托运单码
				myCp: {}, // 记录扫描出的优惠券对象
				qr_src_data: '', // 记录扫码的原始数据 
			}
		},

		onLoad(opts) {
			this.type = `${opts.type}`
			// this.reqCmmOrderStatus('69556_39899063842')
		},

		mounted() {
			let that = this;
			if (that.type == 1) { // 优惠券码
				uni.setNavigationBarTitle({ title: '扫描优惠券码' })
			} else if (that.type == 2) { // 托运单码
				uni.setNavigationBarTitle({ title: '扫描托运单码' })
			}
		},

		methods: {
			qrcodeSucess(data) {
				this.qr_src_data = data
				let that = this
				if (that.type == 1) { // 优惠券码
					try {
						that.myCp = JSON.parse(data)
						uni.$emit('onGetQrCode', { qr_code_data: data, type: that.type })
						uni.showModal({
							title: '提示',
							content: `识别成功！面值:¥${that.myCp.money} 有效期至:${that.myCp.end_time_text}`,
							showCancel: false,
							success: res => {
								if (res.confirm) {
									uni.navigateBack()
								}
							}
						})
					} catch (error) {
						uni.showModal({
							title: '提示',
							content: `请扫描正确的优惠券码!`,
							showCancel: false,
							success: res => {
								if (res.confirm) {
									uni.navigateBack()
								}
							}
						})
					}
				} else if (that.type == 2) { // 托运单码
					try {
						// 解析托运单码数据,获取qr字段的值
						if (data && data.includes('qr=')) {
							let t1 = data.split('qr=')[1]
							if (t1 && (t1.endsWith('"') || t1.endsWith('”') || t1.endsWith('“'))) {
								t1 = t1.slice(0, -1)
							}
							// 根据qr字段的值,获取运单详情
							that.reqCmmOrderStatus(t1)
						} else {
							uni.showModal({
								title: '提示',
								content: `请扫描正确的托运单码!`,
								showCancel: false,
								success: res => {
									if (res.confirm) {
										uni.navigateBack()
									}
								}
							})
						}
					} catch (error) {
						uni.showModal({
							title: '提示',
							content: `请扫描正确的托运单码!`,
							showCancel: false,
							success: res => {
								if (res.confirm) {
									uni.navigateBack()
								}
							}
						})
					}
				}

			},

			qrcodeError(err) {
				console.log(err)
				uni.showModal({
					title: '摄像头授权失败',
					content: '摄像头授权失败，请检测当前浏览器是否有摄像头权限。',
					success: res => {
						uni.navigateBack({})
					}
				})
			},

			// 接口:车满满获取运单详情 69556_39899063842
			async reqCmmOrderStatus(order_sn) {
				let that = this
				let token = getUInfo().token
				uni.showLoading({ title: '查询中', mask: true, })
				let res = await that.$getOrder({ token, order_sn })
				if (res.code == 1) { // 业务请求成功

					// uni.showModal({ title: '调试', content: `请求成功= ${JSON.stringify(res)}` })
					let { startCity, toCity, od_num } = res.data
					console.log('发站-到站-运单号 = ', `${startCity}-${toCity}-${od_num}`);

					let order_obj = res.data
					// 处理货物名称和数量
					if (order_obj.goods && order_obj.goods.length > 0) {
						order_obj['goods_info'] = `${order_obj.goods[0].name}${order_obj.goods[0].num}件`
					} else {
						order_obj['goods_info'] = '未填写货物信息'
					}
					// 增加二维码内容字段
					order_obj['qr_code'] = order_sn
					uni.$emit('onGetQrCode', { qr_code_data: JSON.stringify(order_obj), type: that.type })
					uni.showModal({
						title: '提示',
						content: `识别成功！发站:${startCity} 到站:${toCity} 单号:${od_num}`,
						showCancel: false,
						success: res => {
							if (res.confirm) {
								uni.navigateBack()
							}
						}
					})
				} else {
					uni.showToast({ icon: 'none', title: `${res.msg}`, duration: 2500 })
				}
			},

		}
	}
</script>

<style scoped>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
</style>